package com.next.aappublicapp;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.next.aap.client.AssemblyConstituencyWithCandidate;
import com.next.aap.client.CandidateDto;
import com.next.aappublicapp.adapters.CandidateListAdapter;
import com.next.aappublicapp.server.service.DataServices;
import com.next.aappublicapp.tasks.AapAsyncTask;
import com.next.aappublicapp.util.TrackerUtl;

public class CandidateListActivity extends BaseListActivty {

	public static final String ARGS_AC = "ARGS_AC";
	public static final String ARGS_BUNDLE = "ARGS_BUNDLE";

	private LinearLayout loadinStatus;
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_candidate_list);

		Bundle bundle = getIntent().getBundleExtra(ARGS_BUNDLE);
		AssemblyConstituencyWithCandidate selectedAssemblyConstituencyWithCandidate = (AssemblyConstituencyWithCandidate) bundle
				.getSerializable(ManifestoActivity.ARGS_AC);

		loadinStatus = (LinearLayout) findViewById(R.id.loading_status);
		listView = (ListView) findViewById(android.R.id.list);
		loadCandidatesOfConstituencyAsync(selectedAssemblyConstituencyWithCandidate
				.getId());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.candidate_list, menu);
		return true;
	}

	private void loadCandidatesOfConstituencyAsync(final long acId) {
		new AapAsyncTask<String, String, List<CandidateDto>>("Candidate", "Get") {
			@Override
			protected void beforeExecute() {
				loadinStatus.setVisibility(View.VISIBLE);
				listView.setVisibility(View.GONE);
			}

			@Override
			protected List<CandidateDto> doInBackground(String... params) {
				List<CandidateDto> candidates = null;
				try {
					candidates = DataServices.getInstance()
							.getAssemblyConstituencyCandidates(
									CandidateListActivity.this, acId);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				return candidates;
			}

			@Override
			protected void afterExecute(List<CandidateDto> result) {
				loadinStatus.setVisibility(View.GONE);
				listView.setVisibility(View.VISIBLE);
				if (result != null) {
					CandidateListActivity.this
							.setListAdapter(new CandidateListAdapter(
									CandidateListActivity.this, result));
				}
			}
		}.execute();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		CandidateDto selectedCandidate = ((CandidateDto) getListAdapter()
				.getItem(position));
		Intent intent = new Intent(this, CandidateProfileActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable(CandidateProfileActivity.ARGS_CANDIDATE,
				selectedCandidate);
		intent.putExtra(CandidateProfileActivity.ARGS_BUNDLE, bundle);
		this.startActivity(intent);
		TrackerUtl.sendAapEvent("CandidateInfo");
	}
}
